package com.jivesoftware.spark.managers
{
	public class StringUtils
	{
		public static function unescapeHTML(string:String):String
		{
			string = string.replace(/[\r\n]/g, "<br>");
			string = string.replace(/\\\\/g, "\\");
			string = string.replace(/&lt;/g, "<");
			string = string.replace(/&gt;/g, ">");
			string = string.replace(/&quot;/g, '"');
			string = string.replace(/&amp;/g, "&");
			
			return string;
		}
		
		private static var quoteregex:RegExp = new RegExp('"', "g");
		
		public static function escapeHTML(string:String):String
		{
			string = string.replace(/<br>/g, "\n");
			string = string.replace(/&/g, "&amp");
			string = string.replace(/</g, "&lt");
			string = string.replace(/>/g, "&gt");
			string = string.replace(quoteregex, "&quot;");
			
			return string;
		}
		
		private static var alphabet:String = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY';
		
		public static function randomString(length:int):String
		{
			var result:String = "";
			while(length--)
			{
				result += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
			}
			return result;
		}
	}
}
